package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.api.vo.artist.ArtistAlbumVo;
import org.jeecg.modules.api.vo.artist.ArtistDetailVo;
import org.jeecg.modules.api.vo.artist.ArtistVo;
import org.jeecg.modules.api.vo.sys.IndexChartVo;
import org.jeecg.modules.music.entity.MusArtist;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.dto.HomeMusCountDto;
import org.jeecg.modules.music.mapper.*;
import org.jeecg.modules.music.service.IMusArtistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MusArtistServiceImpl extends ServiceImpl<MusArtistMapper, MusArtist> implements IMusArtistService {


    @Autowired
    private MusAlbumSongListMapper albumSongListMapper;

    @Autowired
    private MusTopListMapper topListMapper;

    @Autowired
    private MusSongMapper musSongMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveArtist(MusArtist artist) {

        this.save(artist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editArtist(MusArtist artist) {
        //step.1 修改歌手基础信息
        this.updateById(artist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteArtist(String id) {

        this.removeById(id);
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchArtists(String ids) {
        //1.删除用户
        this.removeByIds(Arrays.asList(ids.split(",")));
        return false;
    }

    @Override
    public IPage<MusSong> querySongDetailPage(Page<MusSong> page, String artistId, MusSong songParams) {
        return this.baseMapper.querySongDetailPage(page,artistId,songParams);
    }

    @Override
    public ArtistVo queryArtistVoById(String artistId) {
        return this.baseMapper.queryArtistVoById(artistId);
    }

    @Override
    public ArtistDetailVo queryArtistDetail(String id) {

        ArtistDetailVo artist = baseMapper.queryArtistDetail(id);
        String[] ids = artist.getCategory().split(",");
        artist.setIdentities(baseMapper.getIdentities(ids));
        artist.setAlbumSize(baseMapper.countAlbums(id));
        artist.setMusicSize(baseMapper.countSong(id));
        return artist;
    }

    @Override
    public List<ArtistAlbumVo> queryArtistAlbums(String id, Integer limit, Integer offset) {
        return baseMapper.queryArtistAlbums(id,limit,offset);
    }

    @Override
    public List<ArtistDetailVo> querySimiSingers(String id) {
        MusArtist artist = baseMapper.selectById(id);
        List<ArtistDetailVo> artists = baseMapper.querySimiSingers(id,artist.getSingerType());
        artists.stream().map(ArtistDetailVo ->{
            ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
            ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(id));
            ArtistDetailVo.setMusicSize(baseMapper.countSong(id));
            return ArtistDetailVo;
        }).collect(Collectors.toList());
        return artists;
    }

    @Override
    public List<ArtistDetailVo> queryHotSingers(Integer offset, Integer limit) {
        List<ArtistDetailVo> artists = baseMapper.queryHotSingers(offset,limit);
        artists.stream().map(ArtistDetailVo ->{
            ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
            ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
            ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
            return ArtistDetailVo;
        }).collect(Collectors.toList());
        return artists;
    }

    @Override
    public List<ArtistDetailVo> querySingersList(Integer type, Integer area, String initial, Integer offset, Integer limit) {
        List<ArtistDetailVo> artists = new ArrayList<>();
        if (initial.equals("0")){
            artists = baseMapper.querySingersList(type,area,null,offset,limit);
            artists.stream().map(ArtistDetailVo ->{
                ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
                ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
                ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
                return ArtistDetailVo;
            }).collect(Collectors.toList());
        }else if (initial.equals("-1")){
            artists = baseMapper.querySingersList(type,area,null,offset,limit);
            artists.stream().map(ArtistDetailVo ->{
                ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
                ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
                ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
                return ArtistDetailVo;
            }).collect(Collectors.toList());
        }else {
            artists = baseMapper.querySingersList(type,area,initial,offset,limit);
            artists.stream().map(ArtistDetailVo ->{
                ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
                ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
                ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
                return ArtistDetailVo;
            }).collect(Collectors.toList());
        }

        return artists;
    }

    @Override
    public IndexChartVo queryIndexChart() {
        IndexChartVo indexChartVo = new IndexChartVo();
        indexChartVo.setCountAlbums(albumSongListMapper.countAlbums());
        indexChartVo.setCountSonglist(albumSongListMapper.countSonglist());
        indexChartVo.setCountSinger(baseMapper.countSingers());
        indexChartVo.setCountToplist(topListMapper.countToplist());
        indexChartVo.setCountSongs(musSongMapper.countSongs());
        return indexChartVo;
    }

    @Override
    public List<ArtistDetailVo> searchSuggestArtist(String keywords) {
        List<ArtistDetailVo> artists = baseMapper.searchSuggestArtist(keywords);
        artists.stream().map(ArtistDetailVo ->{
            ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
            ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
            ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
            return ArtistDetailVo;
        }).collect(Collectors.toList());
        return artists;
    }

    @Override
    public List<ArtistDetailVo> searchArtist(String keywords, Integer limit, Integer offset) {
        List<ArtistDetailVo> artists = baseMapper.searchArtist(keywords,limit,offset);
        artists.stream().map(ArtistDetailVo ->{
            ArtistDetailVo.setIdentities(baseMapper.getIdentities(ArtistDetailVo.getCategory().split(",")));
            ArtistDetailVo.setAlbumSize(baseMapper.countAlbums(ArtistDetailVo.getId()));
            ArtistDetailVo.setMusicSize(baseMapper.countSong(ArtistDetailVo.getId()));
            return ArtistDetailVo;
        }).collect(Collectors.toList());
        return artists;
    }

    @Override
    public List<HomeMusCountDto> queryGroupCountry() {
        return baseMapper.queryGroupCountry();
    }

    @Override
    public List<HomeMusCountDto> queryGroupSex() {
        return baseMapper.queryGroupSex();
    }

    @Override
    public List<HomeMusCountDto> queryGroupIsEnter() {
        return baseMapper.queryGroupIsEnter();
    }
}
